package com.chris.libs.example.pages.ext.smartrefresh;

import com.chris.libs.base.x.mvpp.mvpp.DefaultPresenter;
import com.chris.libs.example.R;
import com.chris.libs.example.model.DataItem;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.inject.Inject;

/**
 * @author Chris Chan
 * Create on： 2025-09-12 15:25
 * Use for: SmartRefresh 业务逻辑处理
 * Explain:
 */
public class SmartRefreshPresenter extends DefaultPresenter<SmartRefreshContract.UIView>
        implements SmartRefreshContract.Presenter {
    // 数据中心
    private SmartRefreshViewModel viewModel;

    // Dagger2注入构造方法
    @Inject
    public SmartRefreshPresenter() {
    }

    // 传入数据中心
    public void setViewModel(SmartRefreshViewModel viewModel) {
        this.viewModel = viewModel;
    }

    // 实例方法，一般由ViewProxy主动调用，必须在契约接口中定义
    @Override
    public void requestAndUpdate() {
        this.viewModel.setInfo("请求并更新的数据");
    }

    @Override
    public void refreshData() {
        List<DataItem> dataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            dataList.add(DataItem.of("标题" + (String.format("%04d", new Random().nextInt(1000))), R.drawable.ic_tab_main_home));
        }
        this.viewModel.setDataList(dataList);
    }

    @Override
    public void loadMoreData(int page, int pageSize) {
        List<DataItem> dataList = new ArrayList<>();
        for (int i = (page - 1) * pageSize; i < pageSize; i++) {
            dataList.add(DataItem.of("标题" + (String.format("%04d", new Random().nextInt(1000))), R.drawable.ic_tab_main_home));
        }
        this.viewModel.setMoreDataList(dataList);
    }
}
